package clientView;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Locale;
import java.util.Vector;

import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.ButtonModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import object.TSM_Dep;
import object.TotalSalaryMonth;
import object.TotalSalaryQuarter;
import object.TotalSalaryYear;
import objectView.DepartmentCom;

import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.entity.StandardEntityCollection;

import uploadServer.PNGorJPGFilter;
import utility.DayMonthYear;
import utility.StringUtil;
import clientMain.Main;
import clientUtilGUI.ChartStatisticSalary;
import clientUtilGUI.UtilityGUI;

import com.toedter.calendar.JMonthChooser;
import com.toedter.calendar.JYearChooser;

import database.TotalSalaryDelegate;

public class StatisticsSS extends JScrollPane implements ActionListener{
	
	private JPanel panel;
	private JPanel panelHeader;
	private JPanel panelBody;
	private JRadioButton rdbtnMonth;
	private JRadioButton rdbtnQuarter;
	private JRadioButton rdbtnYear;
	private ButtonGroup groupCondition1;
	private ButtonGroup groupCondition2;
	private JRadioButton rdbtnInterval;
	private JRadioButton rdbtnBetween;
	private JButton btnView;
	private JButton btnPrintImg;
	private JPanel panelBtn;
	private JPanel panelChart;
	private JPanel panelTime;
	private GridBagConstraints gbc_panelTime;
	private JMonthChooser monthChooser_To;
	private JYearChooser yearChooser_To;
	private JMonthChooser monthChooser_From;
	private JYearChooser yearChooser_From;
	private GridBagConstraints gbc_panelBtn;
	private JComboBox cbbQuarter_To;
	private JComboBox cbbQuarter_From;
	private GridBagConstraints gbc_panelChart;
	private String cond1 = "";
	private String cond2 = "";
	private boolean flag;
	private String myStart;
	private String myEnd;
	private String yStart;
	private String yEnd;
	private String qyStart;
	private String qyEnd;
	private JFileChooser fileChooser;
	private JCheckBox chkbDep1;
	private JCheckBox chkbDep2;
	private JCheckBox chkbDep3;
	private JRadioButton rdbtnCom;
	Vector<DepartmentCom> listDep = Main.viewDep.getListDepInfo(Main.comId, 0, "Dep_Id", true);

	public StatisticsSS() {
		panel = new JPanel();
		setViewportView(panel);
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = new int[]{0};
		gbl_panel.rowHeights = new int[]{0, 01};
		gbl_panel.columnWeights = new double[]{1.0};
		gbl_panel.rowWeights = new double[]{0.0, 1.0};
		panel.setLayout(gbl_panel);
		
		panelHeader = UtilityGUI.headerForm("Thống Kê Tài Chính");
		GridBagConstraints gbc_panelHeader = new GridBagConstraints();
		gbc_panelHeader.insets = new Insets(8, 10, 10, 10);
		gbc_panelHeader.fill = GridBagConstraints.BOTH;
		gbc_panelHeader.gridx = 0;
		gbc_panelHeader.gridy = 0;
		panel.add(panelHeader, gbc_panelHeader);
		
		panelBody = new JPanel();
		GridBagConstraints gbc_panelBody = new GridBagConstraints();
		gbc_panelBody.insets = new Insets(5, 10, 10, 10);
		gbc_panelBody.fill = GridBagConstraints.BOTH;
		gbc_panelBody.gridx = 0;
		gbc_panelBody.gridy = 1;
		panel.add(panelBody, gbc_panelBody);
		GridBagLayout gbl_panelBody = new GridBagLayout();
		gbl_panelBody.columnWidths = new int[]{0, 0, 0, 0, 0};
		gbl_panelBody.rowHeights = new int[]{0, 0, 0, 0, 0, 0};
		gbl_panelBody.columnWeights = new double[]{0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
		gbl_panelBody.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
		panelBody.setLayout(gbl_panelBody);
		
		CondListener cListener = new CondListener();
		JLabel lblCondition1 = new JLabel("Thống Kê Theo :");
		lblCondition1.setFont(UtilityGUI.fontLblForm);
		GridBagConstraints gbc_lblCondition1 = new GridBagConstraints();
		gbc_lblCondition1.anchor = GridBagConstraints.WEST;
		gbc_lblCondition1.insets = new Insets(3, 5, 5, 5);
		gbc_lblCondition1.gridx = 0;
		gbc_lblCondition1.gridy = 0;
		panelBody.add(lblCondition1, gbc_lblCondition1);
		
		rdbtnMonth = new JRadioButton("Tháng");
		rdbtnMonth.setFont(UtilityGUI.fontLblForm);
		rdbtnMonth.addItemListener(cListener);
		GridBagConstraints gbc_rdbtnMonth = new GridBagConstraints();
		gbc_rdbtnMonth.anchor = GridBagConstraints.WEST;
		gbc_rdbtnMonth.insets = new Insets(3, 0, 5, 5);
		gbc_rdbtnMonth.gridx = 1;
		gbc_rdbtnMonth.gridy = 0;
		panelBody.add(rdbtnMonth, gbc_rdbtnMonth);
		
		rdbtnQuarter = new JRadioButton("Quý");
		rdbtnQuarter.setFont(UtilityGUI.fontLblForm);
		rdbtnQuarter.addItemListener(cListener);
		GridBagConstraints gbc_rdbtnQuarter = new GridBagConstraints();
		gbc_rdbtnQuarter.anchor = GridBagConstraints.WEST;
		gbc_rdbtnQuarter.insets = new Insets(3, 0, 5, 5);
		gbc_rdbtnQuarter.gridx = 2;
		gbc_rdbtnQuarter.gridy = 0;
		panelBody.add(rdbtnQuarter, gbc_rdbtnQuarter);
		
		rdbtnYear = new JRadioButton("Năm");
		rdbtnYear.setFont(UtilityGUI.fontLblForm);
		rdbtnYear.addItemListener(cListener);
		GridBagConstraints gbc_rdbtnYear = new GridBagConstraints();
		gbc_rdbtnYear.anchor = GridBagConstraints.WEST;
		gbc_rdbtnYear.insets = new Insets(3, 0, 5, 5);
		gbc_rdbtnYear.gridx = 3;
		gbc_rdbtnYear.gridy = 0;
		panelBody.add(rdbtnYear, gbc_rdbtnYear);
		
		groupCondition1 = new ButtonGroup();
		groupCondition1.add(rdbtnMonth);
		groupCondition1.add(rdbtnQuarter);
		groupCondition1.add(rdbtnYear);
		
		JLabel lblCondition2 = new JLabel("Xem Theo :");
		lblCondition2.setFont(UtilityGUI.fontLblForm);
		GridBagConstraints gbc_lblCondition2 = new GridBagConstraints();
		gbc_lblCondition2.anchor = GridBagConstraints.WEST;
		gbc_lblCondition2.insets = new Insets(3, 5, 5, 5);
		gbc_lblCondition2.gridx = 0;
		gbc_lblCondition2.gridy = 1;
		panelBody.add(lblCondition2, gbc_lblCondition2);
		
		rdbtnInterval = new JRadioButton("Khoảng thời gian");
		rdbtnInterval.setFont(UtilityGUI.fontLblForm);
		rdbtnInterval.addItemListener(cListener);
		GridBagConstraints gbc_rdbtnInterval = new GridBagConstraints();
		gbc_rdbtnInterval.anchor = GridBagConstraints.WEST;
		gbc_rdbtnInterval.gridwidth = 2;
		gbc_rdbtnInterval.insets = new Insets(3, 0, 5, 5);
		gbc_rdbtnInterval.gridx = 1;
		gbc_rdbtnInterval.gridy = 1;
		panelBody.add(rdbtnInterval, gbc_rdbtnInterval);
		
		rdbtnBetween = new JRadioButton("So sánh 2 thời điểm");
		rdbtnBetween.setFont(UtilityGUI.fontLblForm);
		rdbtnBetween.addItemListener(cListener);
		GridBagConstraints gbc_rdbtnBetween = new GridBagConstraints();
		gbc_rdbtnBetween.anchor = GridBagConstraints.WEST;
		gbc_rdbtnBetween.insets = new Insets(3, 0, 5, 5);
		gbc_rdbtnBetween.gridx = 3;
		gbc_rdbtnBetween.gridy = 1;
		panelBody.add(rdbtnBetween, gbc_rdbtnBetween);
		
		groupCondition2 = new ButtonGroup();
		groupCondition2.add(rdbtnInterval);
		groupCondition2.add(rdbtnBetween);
		
		panelTime = new JPanel();
		gbc_panelTime = new GridBagConstraints();
		gbc_panelTime.insets = new Insets(3, 5, 5, 5);
		gbc_panelTime.gridwidth = 4;
		gbc_panelTime.fill = GridBagConstraints.BOTH;
		gbc_panelTime.gridx = 0;
		gbc_panelTime.gridy = 2;
		panelBody.add(panelTime, gbc_panelTime);
		
		panelBtn = new JPanel();
		gbc_panelBtn = new GridBagConstraints();
		gbc_panelBtn.insets = new Insets(3, 5, 5, 5);
		gbc_panelBtn.gridwidth = 4;
		gbc_panelBtn.fill = GridBagConstraints.BOTH;
		gbc_panelBtn.gridx = 0;
		gbc_panelBtn.gridy = 3;		
		panelBody.add(panelBtn, gbc_panelBtn);
		panelBtn.setVisible(false);
		
		btnView = new JButton(UtilityGUI.btnView);
		btnView.setFont(UtilityGUI.fontBtnForm);
		btnView.addActionListener(this);
		panelBtn.add(btnView);
		btnPrintImg = new JButton(UtilityGUI.btnPrintImg);
		btnPrintImg.setFont(UtilityGUI.fontBtnForm);
		btnPrintImg.addActionListener(this);
		panelBtn.add(btnPrintImg);
		
		panelChart = new JPanel();
		gbc_panelChart = new GridBagConstraints();
		gbc_panelChart.gridwidth = 4;
		gbc_panelChart.insets = new Insets(3, 5, 5, 5);
		gbc_panelChart.fill = GridBagConstraints.BOTH;
		gbc_panelChart.gridx = 0;
		gbc_panelChart.gridy = 4;
		panelBody.add(panelChart, gbc_panelChart);
		
	}
	
	private JPanel timeCondition(String a, String b){
		JPanel panel = new JPanel();
		GridBagLayout gbl_panelTime = new GridBagLayout();
		gbl_panelTime.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0};
		gbl_panelTime.rowHeights = new int[]{0, 0, 0};
		gbl_panelTime.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		gbl_panelTime.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
		panel.setLayout(gbl_panelTime);
		
		JLabel lblFrom = new JLabel();
		lblFrom.setFont(UtilityGUI.fontLblForm);
		GridBagConstraints gbc_lblFrom = new GridBagConstraints();
		gbc_lblFrom.insets = new Insets(0, 0, 0, 5);
		gbc_lblFrom.anchor = GridBagConstraints.WEST;
		gbc_lblFrom.gridx = 0;
		gbc_lblFrom.gridy = 0;
		panel.add(lblFrom, gbc_lblFrom);
		if(b.equals("i")){			
			lblFrom.setText("Từ :");
		}else if(b.equals("b")){
			lblFrom.setText("Thời điểm 1:");
		}		
		
		GridBagConstraints gbc_cbbMorQ_From = new GridBagConstraints();
		gbc_cbbMorQ_From.insets = new Insets(0, 0, 0, 5);
		gbc_cbbMorQ_From.anchor = GridBagConstraints.WEST;
		gbc_cbbMorQ_From.gridx = 1;
		gbc_cbbMorQ_From.gridy = 0;
		if(a.equals("m")){
			monthChooser_From = new JMonthChooser();
			monthChooser_From.setBorder(UIManager.getBorder("ComboBox.border"));
			monthChooser_From.setFont(UtilityGUI.fontComboBox);
			monthChooser_From.setLocale(new Locale("vi"));
			monthChooser_From.getComboBox().setPreferredSize(new Dimension(130, 22));
			panel.add(monthChooser_From, gbc_cbbMorQ_From);
		}else if(a.equals("q")){
			cbbQuarter_From = UtilityGUI.cbbQuaterofYear();
			panel.add(cbbQuarter_From, gbc_cbbMorQ_From);
		}		
		
		yearChooser_From = new JYearChooser();
		yearChooser_From.setEndYear(Main.fullday.getYear()+1900);
		yearChooser_From.setStartYear(2000);
		yearChooser_From.setFont(UtilityGUI.fontComboBox);
		GridBagConstraints gbc_cbbY_From = new GridBagConstraints();
		gbc_cbbY_From.insets = new Insets(0, 0, 0, 5);
		gbc_cbbY_From.anchor = GridBagConstraints.WEST;
		gbc_cbbY_From.gridx = 2;
		gbc_cbbY_From.gridy = 0;
		panel.add(yearChooser_From, gbc_cbbY_From);
		
		JLabel lblTo = new JLabel();
		lblTo.setFont(UtilityGUI.fontLblForm);
		GridBagConstraints gbc_lblTo = new GridBagConstraints();
		gbc_lblTo.insets = new Insets(0, 5, 0, 5);
		gbc_lblTo.anchor = GridBagConstraints.WEST;
		gbc_lblTo.gridx = 3;
		gbc_lblTo.gridy = 0;
		panel.add(lblTo, gbc_lblTo);
		if(b.equals("i")){
			lblTo.setText("Đến :");
		}else if(b.equals("b")){
			lblTo.setText("Thời điểm 2:");
		}
		
		GridBagConstraints gbc_cbbMorQ_To = new GridBagConstraints();
		gbc_cbbMorQ_To.insets = new Insets(0, 0, 0, 5);
		gbc_cbbMorQ_To.anchor = GridBagConstraints.WEST;
		gbc_cbbMorQ_To.gridx = 4;
		gbc_cbbMorQ_To.gridy = 0;
		if(a.equals("m")){
			monthChooser_To = new JMonthChooser();
			monthChooser_To.setBorder(UIManager.getBorder("ComboBox.border"));
			monthChooser_To.setFont(UtilityGUI.fontComboBox);
			monthChooser_To.setLocale(new Locale("vi"));
			monthChooser_To.getComboBox().setPreferredSize(new Dimension(130, 22));
			panel.add(monthChooser_To, gbc_cbbMorQ_To);			
		}else if(a.equals("q")){
			cbbQuarter_To = UtilityGUI.cbbQuaterofYear();
			panel.add(cbbQuarter_To, gbc_cbbMorQ_To);
		}		
		
		yearChooser_To = new JYearChooser();
		yearChooser_To.setEndYear(Main.fullday.getYear()+1900);
		yearChooser_To.setStartYear(2000);
		yearChooser_To.setFont(UtilityGUI.fontComboBox);
		GridBagConstraints gbc_cbbY_To = new GridBagConstraints();
		gbc_cbbY_To.insets = new Insets(0, 0, 0, 5);
		gbc_cbbY_To.anchor = GridBagConstraints.WEST;
		gbc_cbbY_To.gridx = 5;
		gbc_cbbY_To.gridy = 0;
		panel.add(yearChooser_To, gbc_cbbY_To);
		
		JLabel lblDep = new JLabel("Phòng:");
		lblDep.setFont(UtilityGUI.fontLblForm);
		GridBagConstraints gbc_lblDep = new GridBagConstraints();
		gbc_lblDep.insets = new Insets(5, 0, 0, 5);
		gbc_lblDep.anchor = GridBagConstraints.WEST;
		gbc_lblDep.gridx = 0;
		gbc_lblDep.gridy = 1;
		panel.add(lblDep, gbc_lblDep);
		
		JPanel panelChkb = new JPanel();
		GridBagConstraints gbc_panelChkb = new GridBagConstraints();
		gbc_panelChkb.insets = new Insets(5, 0, 0, 5);
		gbc_panelChkb.anchor = GridBagConstraints.WEST;
		gbc_panelChkb.gridx = 1;
		gbc_panelChkb.gridy = 1;
		gbc_panelChkb.gridwidth = 5;
		panel.add(panelChkb, gbc_panelChkb);
		
		chkbDep1 = new JCheckBox(listDep.get(0).getDepName());
		chkbDep1.setFont(UtilityGUI.fontLblForm);
		panelChkb.add(chkbDep1);
		
		chkbDep2 = new JCheckBox(listDep.get(1).getDepName());
		chkbDep2.setFont(UtilityGUI.fontLblForm);
		panelChkb.add(chkbDep2);
		
		chkbDep3 = new JCheckBox(listDep.get(2).getDepName());
		chkbDep3.setFont(UtilityGUI.fontLblForm);
		panelChkb.add(chkbDep3);
		
		rdbtnCom = new JRadioButton("Công Ty");
		rdbtnCom.setFont(UtilityGUI.fontLblForm);
		panelChkb.add(rdbtnCom);
		rdbtnCom.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				AbstractButton aButton = (AbstractButton)e.getSource();
		        ButtonModel aModel = aButton.getModel();
		        boolean selected = aModel.isSelected();
		        if(selected){
		        	chkbDep1.setEnabled(false);
		        	chkbDep2.setEnabled(false);
		        	chkbDep3.setEnabled(false);
		        }else{
		        	chkbDep1.setEnabled(true);
		        	chkbDep2.setEnabled(true);
		        	chkbDep3.setEnabled(true);
		        }
			}
		});
		
		return panel;
	}
		
	private class CondListener implements ItemListener {
		public void itemStateChanged(ItemEvent ev) {
			panelTime.setVisible(false);
			panelBtn.setVisible(false);
	        if(ev.getSource() == rdbtnMonth){
				cond1 = "m";
			}else if(ev.getSource() == rdbtnQuarter){
				cond1 = "q";
			}else if(ev.getSource() == rdbtnYear){
				cond1 = "y";
			}
	        if(ev.getSource() == rdbtnInterval){
				cond2 = "i";
				flag = true;
			}else if(ev.getSource() == rdbtnBetween){
				cond2 = "b";
				flag = false;
			}
	        if(!cond1.isEmpty() && !cond2.isEmpty()){
				panelTime = timeCondition(cond1, cond2);
				panelBody.add(panelTime, gbc_panelTime);
				panelBtn.setVisible(true);
			}
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		ChartStatisticSalary chart = null;
		if(e.getSource() == btnView){
			String listDepId = "";
			Vector<String> listDepName = new Vector<String>();
			int comId = 0;
			if(!(rdbtnCom.isSelected() || chkbDep1.isSelected() || chkbDep2.isSelected() || chkbDep3.isSelected())){
				JOptionPane.showMessageDialog(null, "Chọn ít nhất 1 phòng ban!", 
									UtilityGUI.titleAlert, JOptionPane.ERROR_MESSAGE);
				return;
			}else{
				if(rdbtnCom.isSelected()){			
					comId = Main.comId;
				}else{
					if(chkbDep1.isSelected()){
						listDepName.add(listDep.get(0).getDepName());
						if(listDepId.isEmpty()) listDepId = Integer.toString(listDep.get(0).getDepId());
						else listDepId = listDepId + "," + Integer.toString(listDep.get(0).getDepId());
					}
					if(chkbDep2.isSelected()){
						listDepName.add(listDep.get(1).getDepName());
						if(listDepId.isEmpty()) listDepId = Integer.toString(listDep.get(1).getDepId());
						else listDepId = listDepId + "," + Integer.toString(listDep.get(1).getDepId());
					}
					if(chkbDep3.isSelected()){
						listDepName.add(listDep.get(2).getDepName());
						if(listDepId.isEmpty()) listDepId = Integer.toString(listDep.get(2).getDepId());
						else listDepId = listDepId + "," + Integer.toString(listDep.get(2).getDepId());
					}
				}
			}
			TotalSalaryDelegate tsd = new TotalSalaryDelegate();			
			if(cond1.equals("m")){
				myStart = DayMonthYear.monthYear(monthChooser_From.getMonth()+1, yearChooser_From.getYear());
				myEnd =  DayMonthYear.monthYear(monthChooser_To.getMonth()+1, yearChooser_To.getYear());
				if((monthChooser_From.getMonth() > monthChooser_To.getMonth() 
						&& yearChooser_From.getYear() == yearChooser_To.getYear()) 
						|| yearChooser_From.getYear() > yearChooser_To.getYear()){
					String temp = myStart;
					myStart = myEnd;
					myEnd = temp;
				}
				if(rdbtnCom.isSelected()){
					Vector<TotalSalaryMonth> tsm = tsd.getTSM(myStart, myEnd, flag);
					Vector<String> m = new Vector<String>();
					Vector<Integer> ts = new Vector<Integer>();
					for(int i =0; i<tsm.size(); i++){
						m.add(tsm.get(i).getMonthYear());
						ts.add(tsm.get(i).getTotalSalary());
					}
					chart = new ChartStatisticSalary(m, ts, "Tháng");
				}else{
					Vector<TSM_Dep> tsm = tsd.getTSM_Dep(comId, listDepId, myStart, myEnd, flag);
					int count = tsm.size()/(StringUtil.subStr(listDepId, ",").size());
					listDepName = StringUtil.duplicate(listDepName, count);
					Vector<String> m = new Vector<String>();
					Vector<Integer> ts = new Vector<Integer>(); 
					for(int i =0; i<tsm.size(); i++){
						m.add(tsm.get(i).getMonthYear());
						ts.add(tsm.get(i).getTotalSalary());
					}
					chart = new ChartStatisticSalary(m, ts, listDepName, "Tháng");
				}
			}else if(cond1.equals("q")){
				qyStart = DayMonthYear.quaterYear(cbbQuarter_From.getSelectedIndex(), yearChooser_From.getYear());
				qyEnd = DayMonthYear.quaterYear(cbbQuarter_To.getSelectedIndex(), yearChooser_To.getYear());
				if((cbbQuarter_From.getSelectedIndex() > cbbQuarter_To.getSelectedIndex()
						&& yearChooser_From.getYear() == yearChooser_To.getYear()) 
						|| yearChooser_From.getYear() > yearChooser_To.getYear()){
					String temp = qyStart;
					qyStart = qyEnd;
					qyEnd = temp;
				}
				Vector<TotalSalaryQuarter> tsq = tsd.getTSQ(qyStart, qyEnd, flag);
				Vector<String> q = new Vector<String>();
				Vector<Integer> ts = new Vector<Integer>();
				for(int i =0; i<tsq.size(); i++){
					q.add(tsq.get(i).getQuarterYear());
					ts.add(tsq.get(i).getTotalSalary());
				}
				chart = new ChartStatisticSalary(q, ts, "Quý");
			}else if(cond1.equals("y")){
				yStart = Integer.toString(yearChooser_From.getYear());
				yEnd = Integer.toString(yearChooser_To.getYear());
				if(yearChooser_From.getYear() > yearChooser_To.getYear()){
					String temp = yStart;
					yStart = yEnd;
					yEnd = temp;
				}
				Vector<TotalSalaryYear> tsy = tsd.getTSY(yStart, yEnd, flag);
				Vector<String> y = new Vector<String>();
				Vector<Integer> ts = new Vector<Integer>();
				for(int i =0; i<tsy.size(); i++){
					y.add(tsy.get(i).getYear());
					ts.add(tsy.get(i).getTotalSalary());
				}
				chart = new ChartStatisticSalary(y, ts, "Năm");
			}
			panelChart.setVisible(false);
			panelChart = new JPanel();
			panelChart.add(chart);
			panelBody.add(panelChart, gbc_panelChart);
		}else if(e.getSource() == btnPrintImg){
			if(chart == null){
				btnView.doClick();
			}
			fileChooser = new JFileChooser();
			fileChooser.addChoosableFileFilter(new PNGorJPGFilter());
			fileChooser.setAcceptAllFileFilterUsed(false);
			int returnVal = fileChooser.showSaveDialog(this);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
	            File file = fileChooser.getSelectedFile();
	            if(file.exists()){
	            	int option = JOptionPane.showConfirmDialog(null, "File '"+file.getName()+"' đã tồn tại.\nBạn sẽ ghi đè lên file cũ?",
	            			UtilityGUI.titleAlert, JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE);
	            	if (option == JOptionPane.NO_OPTION) {
						btnPrintImg.doClick();
						return;
					}
	            }
	            String fullPath = file.getAbsolutePath();
	            String extension;
	            if(StringUtil.getExtension(fullPath).equalsIgnoreCase("png")){
	            	extension = "png";
	            }else if(StringUtil.getExtension(fullPath).equalsIgnoreCase("jpg")){
	            	extension = "jpg";
	            }else{
	            	fullPath +=".png";
	            	extension = "png";
	            }
	            File fileSave = new File(fullPath);
	            ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
	            OutputStream out;
				try {
					out = new BufferedOutputStream(new FileOutputStream(fileSave));
					if(extension.equals("png")){
						ChartUtilities.writeChartAsPNG(out, chart.chart, 600, 400, info);
					}else if(extension.equals("jpg")){
						ChartUtilities.writeChartAsJPEG(out, 1.0f, chart.chart, 600, 400, info);
					}
					file = null;
					fileSave = null;
				} catch (FileNotFoundException e1) {
					e1.printStackTrace();
				} catch (IOException e2) {
					e2.printStackTrace();
				}
            }			
		}
	}
}
